{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Semantic Kernel Planner\n",
    "\n",
    "在[chatbot][./chatbot_with_sk.ipynb]中我们介绍\n",
    "在Semantic-Kernel（以下简称SK）中，有若干可以用于实现Plan的Planner类型：\n",
    "- SequentialPlanner：整体规划多步plan，在串联进行执行\n",
    "- ActionPlanner：创建单个Action的Planner。\n",
    "- StepwisePlanner：根据大模型的响应，逐步的进行计划和执行，类似于ReAct"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "接下来我们来了解如何结合qianfan + planner实现一个简单的demo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 前置准备\n",
    "### 安装依赖：\n",
    "本文基于semantic-kernel `0.4.5dev0` 版本，由于 SK持续迭代的原因，原来的Skill正在迁移成Plugin，如碰到不兼容问题请检查依赖版本。\n",
    "使用以下命令可以安装我们所需要的`qianfan` 以及 `semantic-kernel`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#-# cell_skip\n",
    "! pip install \"qianfan>=0.3.0\" -U\n",
    "! pip install semantic-kernel=='0.4.5.dev0'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与直接调用千帆SDK类似，我们需要先初始化鉴权(以下以使用IAM鉴权为例)："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import os\n",
    "\n",
    "os.environ[\"QIANFAN_AK\"] = \"your_ak\"\n",
    "os.environ[\"QIANFAN_SK\"] = \"your_sk\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "初始化一个SK `kernel`， kernel是 SK中的一个重要类型，通过Kernel，我们可以把众多Plugin，LLM，以及Memory等进行注册组合，最终实现一键式的规划调用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import semantic_kernel as sk\n",
    "kernel = sk.Kernel()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以下是使用`ActionPlanner`实现的计算的一个例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copyright (c) Microsoft. All rights reserved.\n",
    "import semantic_kernel as sk\n",
    "from qianfan.extensions.semantic_kernel import QianfanChatCompletion\n",
    "from semantic_kernel.core_skills import FileIOSkill, MathSkill, TextSkill, TimeSkill\n",
    "from semantic_kernel.planning import ActionPlanner\n",
    "\n",
    "\n",
    "kernel = sk.Kernel()\n",
    "\n",
    "kernel.add_chat_service(\"erniebot\", QianfanChatCompletion(\"ERNIE-Bot-4\"))\n",
    "kernel.import_skill(MathSkill(), \"math\")\n",
    "kernel.import_skill(FileIOSkill(), \"fileIO\")\n",
    "kernel.import_skill(TimeSkill(), \"time\")\n",
    "kernel.import_skill(TextSkill(), \"text\")\n",
    "\n",
    "# create an instance of action planner.\n",
    "planner = ActionPlanner(kernel)\n",
    "\n",
    "# the ask for which the action planner is going to find a relevant function.\n",
    "ask = \"What is the sum of 110 and 990?\"\n",
    "\n",
    "# ask the action planner to identify a suitable function from the list of functions available.\n",
    "plan = await planner.create_plan_async(goal=ask)\n",
    "\n",
    "# ask the action planner to execute the identified function.\n",
    "result = await plan.invoke_async()\n",
    "print(result)\n",
    "\"\"\"\n",
    "Output:\n",
    "1100\n",
    "\"\"\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "vscode": {
   "interpreter": {
    "hash": "58f7cb64c3a06383b7f18d2a11305edccbad427293a2b4afa7abe8bfc810d4bb"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
